home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / nos042_s / udp.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  7KB  |  299 lines

  1. /* Internet User Data Protocol (UDP)
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  
  5. /****************************************************************************
  6. *    $Id: udp.c 1.2 93/07/16 11:51:57 ROOT_DOS Exp $
  7. *    09 May 93    1.2        GT    Fix warnings.                                    *
  8. *
  9. *  ATARI Version by David Nash - dnash@chaos.demon.co.uk
  10. *
  11. ****************************************************************************/
  12.  
  13. #include "global.h"
  14. #include "mbuf.h"
  15. #include "netuser.h"
  16. #include "iface.h"
  17. #include "udp.h"
  18. #include "ip.h"
  19. #include "internet.h"
  20. #include "icmp.h"
  21.  
  22. static struct udp_cb *lookup_udp __ARGS((struct socket *socket));
  23.  
  24. struct mib_entry Udp_mib[] = {
  25.     { "",            0 },
  26.     { "udpInDatagrams",    0 },
  27.     { "udpNoPorts",        0 },
  28.     { "udpInErrors",        0 },
  29.     { "udpOutDatagrams",    0 },
  30. };
  31.  
  32. /* UDP control structures list */
  33. struct udp_cb *Udps;
  34.  
  35. /* Create a UDP control block for lsocket, so that we can queue
  36.  * incoming datagrams.
  37.  */
  38. struct udp_cb *
  39. open_udp(lsocket,r_upcall)
  40. struct socket *lsocket;
  41. void (*r_upcall)();
  42. {
  43.     register struct udp_cb *up;
  44.  
  45.     if((up = lookup_udp(lsocket)) != NULLUDP){
  46.         /* Already exists */
  47.         Net_error = CON_EXISTS;
  48.         return NULLUDP;
  49.     }
  50.     up = (struct udp_cb *)callocw(1,sizeof (struct udp_cb));
  51.     up->socket.address = lsocket->address;
  52.     up->socket.port = lsocket->port;
  53.     up->r_upcall = r_upcall;
  54.  
  55.     up->next = Udps;
  56.     Udps = up;
  57.     return up;
  58. }
  59.  
  60. /* Send a UDP datagram */
  61.  
  62. int send_udp(
  63.     struct socket *lsocket,        /* Source socket */
  64.     struct socket *fsocket,        /* Destination socket */
  65.     char tos,                        /* Type-of-service for IP */
  66.     char ttl,                        /* Time-to-live for IP */
  67.     struct mbuf *data,            /* Data field, if any */
  68.     int16 length,                    /* Length of data field */
  69.     int16 id,                        /* Optional ID field for IP */
  70.     char df )                        /* Don't Fragment flag for IP */
  71. {
  72.     struct mbuf *bp;
  73.     struct pseudo_header ph;
  74.     struct udp udp;
  75.     int32 laddr;
  76.  
  77.     if(length != 0 && data != NULLBUF)
  78.         trim_mbuf(&data,length);
  79.     else
  80.         length = len_p(data);
  81.  
  82.     length += UDPHDR;
  83.  
  84.     laddr = lsocket->address;
  85.     if(laddr == INADDR_ANY)
  86.         laddr = locaddr(fsocket->address);
  87.  
  88.     udp.source = lsocket->port;
  89.     udp.dest = fsocket->port;
  90.     udp.length = length;
  91.  
  92.     /* Create IP pseudo-header, compute checksum and send it */
  93.     ph.length = length;
  94.     ph.source = laddr;
  95.     ph.dest = fsocket->address;
  96.     ph.protocol = UDP_PTCL;
  97.  
  98.     if((bp = htonudp(&udp,data,&ph)) == NULLBUF){
  99.         Net_error = NO_MEM;
  100.         free_p(data);
  101.         return 0;
  102.     }
  103.     udpOutDatagrams++;
  104.     ip_send(laddr,fsocket->address,UDP_PTCL,tos,ttl,bp,length,id,df);
  105.     return (int)length;
  106. }
  107. /* Accept a waiting datagram, if available. Returns length of datagram */
  108. int
  109. recv_udp(up,fsocket,bp)
  110. register struct udp_cb *up;
  111. struct socket *fsocket;        /* Place to stash incoming socket */
  112. struct mbuf **bp;        /* Place to stash data packet */
  113. {
  114.     struct socket sp;
  115.     struct mbuf *buf;
  116.     int16 length;
  117.  
  118.     if(up == NULLUDP){
  119.         Net_error = NO_CONN;
  120.         return -1;
  121.     }
  122.     if(up->rcvcnt == 0){
  123.         Net_error = WOULDBLK;
  124.         return -1;
  125.     }
  126.     buf = dequeue(&up->rcvq);
  127.     up->rcvcnt--;
  128.  
  129.     /* Strip socket header */
  130.     pullup(&buf,(char *)&sp,sizeof(struct socket));
  131.  
  132.     /* Fill in the user's foreign socket structure, if given */
  133.     if(fsocket != NULLSOCK){
  134.         fsocket->address = sp.address;
  135.         fsocket->port = sp.port;
  136.     }
  137.     /* Hand data to user */
  138.     length = len_p(buf);
  139.     if(bp != NULLBUFP)
  140.         *bp = buf;
  141.     else
  142.         free_p(buf);
  143.     return (int)length;
  144. }
  145. /* Delete a UDP control block */
  146. int
  147. del_udp(conn)
  148. struct udp_cb *conn;
  149. {
  150.     struct mbuf *bp;
  151.     register struct udp_cb *up;
  152.     struct udp_cb *udplast = NULLUDP;
  153.  
  154.     for(up = Udps;up != NULLUDP;udplast = up,up = up->next){
  155.         if(up == conn)
  156.             break;
  157.     }
  158.     if(up == NULLUDP){
  159.         /* Either conn was NULL or not found on list */
  160.         Net_error = INVALID;
  161.         return -1;
  162.     }
  163.     /* Get rid of any pending packets */
  164.     while(up->rcvcnt != 0){
  165.         bp = up->rcvq;
  166.         up->rcvq = up->rcvq->anext;
  167.         free_p(bp);
  168.         up->rcvcnt--;
  169.     }
  170.     /* Remove from list */
  171.     if(udplast != NULLUDP)
  172.         udplast->next = up->next;
  173.     else
  174.         Udps = up->next;    /* was first on list */
  175.  
  176.     free((char *)up);
  177.     return 0;
  178. }
  179. /* Process an incoming UDP datagram */
  180. void
  181. udp_input(iface,ip,bp,rxbroadcast)
  182. struct iface *iface;    /* Input interface */
  183. struct ip *ip;        /* IP header */
  184. struct mbuf *bp;    /* UDP header and data */
  185. int rxbroadcast;    /* The only protocol that accepts 'em */
  186. {
  187.     struct pseudo_header ph;
  188.     struct udp udp;
  189.     struct udp_cb *up;
  190.     struct socket lsocket;
  191.     struct socket fsocket;
  192.     struct mbuf *packet;
  193.     int16 length;
  194.  
  195.     if(bp == NULLBUF)
  196.         return;
  197.  
  198.     /* Create pseudo-header and verify checksum */
  199.     ph.source = ip->source;
  200.     ph.dest = ip->dest;
  201.     ph.protocol = ip->protocol;
  202.     length = ip->length - IPLEN - ip->optlen;
  203.     ph.length = length;
  204.  
  205.     /* Peek at header checksum before we extract the header. This
  206.      * allows us to bypass cksum() if the checksum field was not
  207.      * set by the sender.
  208.      */
  209.     udp.checksum = udpcksum(bp);
  210.     if(udp.checksum != 0 && cksum(&ph,bp,length) != 0){
  211.         /* Checksum non-zero, and wrong */
  212.         udpInErrors++;
  213.         free_p(bp);
  214.         return;
  215.     }
  216.     /* Extract UDP header in host order */
  217.     if(ntohudp(&udp,&bp) != 0){
  218.         /* Truncated header */
  219.         udpInErrors++;
  220.         free_p(bp);
  221.         return;
  222.     }
  223.     /* If this was a broadcast packet, pretend it was sent to us */
  224.     if(rxbroadcast){
  225.         lsocket.address = iface->addr;
  226.     } else
  227.         lsocket.address = ip->dest;
  228.  
  229.     lsocket.port = udp.dest;
  230.     /* See if there's somebody around to read it */
  231.     if((up = lookup_udp(&lsocket)) == NULLUDP){
  232.         /* Nope, return an ICMP message */
  233.         if(!rxbroadcast){
  234.             bp = htonudp(&udp,bp,&ph);
  235.             icmp_output(ip,bp,ICMP_DEST_UNREACH,ICMP_PORT_UNREACH,NULL);
  236.         }
  237.         udpNoPorts++;
  238.         free_p(bp);
  239.         return;
  240.     }
  241.     /* Create space for the foreign socket info */
  242.     if((packet = pushdown(bp,sizeof(fsocket))) == NULLBUF){
  243.         /* No space, drop whole packet */
  244.         free_p(bp);
  245.         udpInErrors++;
  246.         return;
  247.     }
  248.     fsocket.address = ip->source;
  249.     fsocket.port = udp.source;
  250.     memcpy(&packet->data[0],(char *)&fsocket,sizeof(fsocket));
  251.  
  252.     /* Queue it */
  253.     enqueue(&up->rcvq,packet);
  254.     up->rcvcnt++;
  255.     udpInDatagrams++;
  256.     if(up->r_upcall)
  257.         (*up->r_upcall)(iface,up,up->rcvcnt);
  258. }
  259. /* Look up UDP socket. 
  260.  * Return control block pointer or NULLUDP if nonexistant
  261.  * As side effect, move control block to top of list to speed future
  262.  * searches.
  263.  */
  264. static struct udp_cb *
  265. lookup_udp(socket)
  266. struct socket *socket;
  267. {
  268.     register struct udp_cb *up;
  269.     struct udp_cb *uplast = NULLUDP;
  270.  
  271.     for(up = Udps;up != NULLUDP;uplast = up,up = up->next){
  272.         if(socket->port == up->socket.port
  273.          && (socket->address == up->socket.address
  274.          || up->socket.address == INADDR_ANY)){
  275.             if(uplast != NULLUDP){
  276.                 /* Move to top of list */
  277.                 uplast->next = up->next;
  278.                 up->next = Udps;
  279.                 Udps = up;
  280.             }
  281.             return up;
  282.         }
  283.     }
  284.     return NULLUDP;
  285. }
  286.  
  287. /* Attempt to reclaim unused space in UDP receive queues */
  288. void
  289. udp_garbage(red)
  290. int red;
  291. {
  292.     register struct udp_cb *udp;
  293.  
  294.     for(udp = Udps;udp != NULLUDP; udp = udp->next){
  295.         mbuf_crunch(&udp->rcvq);
  296.     }
  297. }
  298.  
  299.